package com.zhenyi.tuangou.task;

import java.util.Date;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.common.model.TgOrder;
import com.zhenyi.tuangou.index.OrderSingleton;

public class CloseOrderTask implements Runnable {

	/**
	 * 根据订单开始时间设定订单状态，超过五分钟订单关闭。
	 */
	@Override
	public void run() {

		OrderSingleton orderSingleto = OrderSingleton.getintsance();
		if (orderSingleto.isOn()) {
			return;
		}
		orderSingleto.turnOff();
		SqlPara sqlPara = Db.getSqlPara("findOrderNoPay");
		List<Record> items = Db.find(sqlPara);
		if (items == null) {
			return;
		}
		for (Record record : items) {
			Long id = record.getLong("id");
			TgOrder order = TgOrder.dao.findById(id);
			Date nowTime = new Date();
			Date startTime = order.getStartTime();
			long diff = nowTime.getTime() - startTime.getTime();
			if (diff < 60000) {
				continue;
			}
			order.setStatus(TgOrder.STATUS_CLOSE);// 关闭订单
			order.update();
		}
		orderSingleto.turnOn();
	}
}
